package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.StudyListBo;
import com.hzncc.scaffold.entity.Bo.SubAdminBo;
import com.hzncc.scaffold.entity.Other.*;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.*;
import com.hzncc.scaffold.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserService userService;

    @Autowired
    UsertagService usertagService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserclassService userclassService;

    @Autowired
    ClassesService classesService;

    @Autowired
    CourseService courseService;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private
    TaskprogressMapper taskprogressMapper;

    @Autowired
    PaperrecordMapper paperrecordMapper;

    @Override
    public ResultT addUser(AddUserVo addUserVo) {

//        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(StrUtil.isNotEmpty(addUserVo.getTelephone()), User::getTelephone, addUserVo.getTelephone());
//        List<User> userList = userService.list(lqw);
//        if (CollUtil.isNotEmpty(userList)) {
//            return ResultT.fail("该号码已存在");
//        }

        LambdaQueryWrapper<User> lqww = new LambdaQueryWrapper<>();
        lqww.eq(StrUtil.isNotEmpty(addUserVo.getIdCardNum()), User::getIdCardNum, addUserVo.getIdCardNum());
        List<User> userListTwo = userService.list(lqww);
        if (CollUtil.isNotEmpty(userListTwo)) {
            return ResultT.fail("该身份证已存在");
        }

        if (!IdcardUtil.isValidCard(addUserVo.getIdCardNum())) {
            return ResultT.fail("身份证号码错误");
        }

        if (!PhoneUtil.isMobile(addUserVo.getTelephone())) {
            return ResultT.fail("手机号码错误");
        }

        User user = BeanUtil.toBean(addUserVo, User.class);
        String gensalt = BCrypt.gensalt();
        String substring = StrUtil.sub(addUserVo.getIdCardNum(), -6, addUserVo.getIdCardNum().length());
        String hashpw = BCrypt.hashpw(substring, gensalt);

        if (addUserVo.getType() == 0) {
            user.setRole("教师");
        }
        if (addUserVo.getType() == 1) {
            user.setRole("学生");
        }
        if (addUserVo.getType() == 2) {
            user.setRole("企业人员");
        }

        user.setInfo("");
        user.setEmail("");
        user.setAccount(addUserVo.getTelephone());
        user.setPassword(hashpw);
        user.setName(addUserVo.getRealname());

        boolean save = userService.save(user);
        if (save) {

            Usertag usertag = new Usertag();
            usertag.setUserId(user.getId());
            if (addUserVo.getType() == 0) {
                usertag.setTagId(3);
            }
            if (addUserVo.getType() == 1) {
                usertag.setTagId(2);
            }
            if (addUserVo.getType() == 2) {
                usertag.setTagId(6);
            }

            usertagService.save(usertag);

            return ResultT.success(user);
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @Override
    public ResultT addUserNoPhone(AddUserVo addUserVo) {

        LambdaQueryWrapper<User> lqww = new LambdaQueryWrapper<>();
        lqww.eq(StrUtil.isNotEmpty(addUserVo.getIdCardNum()), User::getIdCardNum, addUserVo.getIdCardNum());
        List<User> userListTwo = userService.list(lqww);
        if (CollUtil.isNotEmpty(userListTwo)) {
            return ResultT.fail("该身份证已存在");
        }

        if (!IdcardUtil.isValidCard(addUserVo.getIdCardNum())) {
            return ResultT.fail("身份证号码错误");
        }

        User user = BeanUtil.toBean(addUserVo, User.class);
        String gensalt = BCrypt.gensalt();
        String substring = StrUtil.sub(addUserVo.getIdCardNum(), -6, addUserVo.getIdCardNum().length());
        String hashpw = BCrypt.hashpw(substring, gensalt);

        if (addUserVo.getType() == 0) {
            user.setRole("教师");
        }
        if (addUserVo.getType() == 1) {
            user.setRole("学生");
        }
        if (addUserVo.getType() == 2) {
            user.setRole("企业人员");
        }

        user.setInfo("");
        user.setEmail("");
        user.setAccount(String.valueOf(System.currentTimeMillis()));
        user.setPassword(hashpw);
        user.setName(addUserVo.getRealname());

        boolean save = userService.save(user);
        if (save) {

            Usertag usertag = new Usertag();
            usertag.setUserId(user.getId());
            if (addUserVo.getType() == 0) {
                usertag.setTagId(3);
            }
            if (addUserVo.getType() == 1) {
                usertag.setTagId(2);
            }
            if (addUserVo.getType() == 2) {
                usertag.setTagId(6);
            }

            usertagService.save(usertag);

            return ResultT.success(user);
        } else {
            return ResultT.fail("新增失败");
        }
    }
    @Override
    public Boolean checkPhone(String telephone) {

        Boolean isExist = false;

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotEmpty(telephone), User::getTelephone, telephone);

        if (CollUtil.isEmpty(userService.list(lqw))) {
            return isExist;
        } else {
            isExist = true;
            return isExist;
        }
    }

    @Override
    @Transactional
    public ResultT importUser(MultipartFile file, Integer type) {
        Integer successNum = 0;
        Integer registerNum = 0;
        Integer allNum = 0;

        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader excelReader = ExcelUtil.getReader(inputStream);
            excelReader.addHeaderAlias("姓名", "realname");
            excelReader.addHeaderAlias("性别", "gender");
            excelReader.addHeaderAlias("身份证号码", "idCardNum");
            excelReader.addHeaderAlias("电话号码", "telephone");
            excelReader.addHeaderAlias("所属院校/单位（非必填）", "instituteName");
            excelReader.addHeaderAlias("所属专业（非必填）", "major");

            List<AddUserVo> addUserVoList = excelReader.readAll(AddUserVo.class);
            allNum = addUserVoList.size();

            for (AddUserVo addUserVo : addUserVoList) {
                if (type == 1) {
                    addUserVo.setType(0);
                }

                if (type == 2) {
                    addUserVo.setType(1);
                }

                if (type == 3) {
                    addUserVo.setType(3);
                }
                ResultT resultT = this.addUser(addUserVo);

                if (resultT.getCode() == 200) {
                    successNum += 1;
                }
                if ("该号码已存在".equals(resultT.getMessage()) || "该身份证已存在".equals(resultT.getMessage())) {
                    registerNum += 1;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResultT.fail("操作失败");
        }
        String message = "共有有效数据" + allNum + "条数据，已注册数据" + registerNum + "条,导入成功" + successNum + "条。";
        return ResultT.success(message);
    }

    @Override
    public ResultT registStudent(RegistStudentVo registStudentVo) {

//        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(User::getTelephone, registStudentVo.getTelephone());
//        List<User> phoneUserList = userService.list(lqw);
//
//        if (CollUtil.isNotEmpty(phoneUserList)) {
//
//            LambdaQueryWrapper<User> lqww = new LambdaQueryWrapper<>();
//            lqw.eq(User::getTelephone, registStudentVo.getTelephone()).last("limit 1");
//            User user = userService.getOne(lqww);
//            return ResultT.success(user);
//        }

        LambdaQueryWrapper<User> lqwww = new LambdaQueryWrapper<>();
        lqwww.eq(User::getIdCardNum, registStudentVo.getIdCardNum());
        List<User> cardUserList = userService.list(lqwww);
        Userclass userClass = new Userclass();
        if (CollUtil.isNotEmpty(cardUserList)) {
            LambdaQueryWrapper<User> lqww = new LambdaQueryWrapper<>();
            lqww.eq(User::getIdCardNum, registStudentVo.getIdCardNum()).last("limit 1");
            User user = userService.getOne(lqww);
//            userClass.setUserId(user.getId());
//            userClass.setClassId(registStudentVo.getClassId());
//            return ResultT.success(userclassService.save(userClass));
            return ResultT.success(user);
        }

        AddUserVo addUserVo = new AddUserVo();
        addUserVo.setType(1);
        addUserVo.setGender(registStudentVo.getGender());
        addUserVo.setTelephone(registStudentVo.getTelephone());
        addUserVo.setRealname(registStudentVo.getRealname());
        addUserVo.setMajor(registStudentVo.getMajor());
        addUserVo.setInstituteName(registStudentVo.getInstituteName());
        addUserVo.setIdCardNum(registStudentVo.getIdCardNum());
        userService.addUser(addUserVo);
        LambdaQueryWrapper<User> lqww = new LambdaQueryWrapper<>();
        lqww.eq(User::getIdCardNum, registStudentVo.getIdCardNum()).last("limit 1");
        User user = userService.getOne(lqww);
//        userClass.setUserId(user.getId());
//        userClass.setClassId(registStudentVo.getClassId());
//        return ResultT.success(userclassService.save(userClass));
        return ResultT.success(user);

    }

    @Override
    @Transactional
    public ResultT importUserToClass(MultipartFile file, Integer classId) {
        importUserToClassVo importUserToClassVo = new importUserToClassVo();
        ArrayList<AddUserPlusVo> list = new ArrayList<>();
        Integer totalCount = 0;
        Integer successCount = 0;
        Integer failedCount = 0;


        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader excelReader = ExcelUtil.getReader(inputStream);
            excelReader.addHeaderAlias("姓名", "realname");
            excelReader.addHeaderAlias("性别", "gender");
            excelReader.addHeaderAlias("电话号码", "telephone");
            excelReader.addHeaderAlias("所属院校（非必填）", "instituteName");
            excelReader.addHeaderAlias("身份证号码", "idCardNum");
            excelReader.addHeaderAlias("所属专业（非必填）", "major");

            List<AddUserPlusVo> addUserVoList = excelReader.readAll(AddUserPlusVo.class);
            totalCount = addUserVoList.size();

            for (AddUserPlusVo addUserVo : addUserVoList) {

                LambdaQueryWrapper<User> lqwOne = new LambdaQueryWrapper<>();
                lqwOne.eq(StrUtil.isNotEmpty(addUserVo.getTelephone()), User::getTelephone, addUserVo.getTelephone());

                LambdaQueryWrapper<User> lqwTwo = new LambdaQueryWrapper<>();
                lqwTwo.eq(StrUtil.isNotEmpty(addUserVo.getIdCardNum()), User::getIdCardNum, addUserVo.getIdCardNum());
                User userTwos = userService.getOne(lqwTwo);   //根据用户的身份证号查找该用户
                if(userTwos !=null){
                    //判断手机号存不存在
                    if(StringUtils.isNotEmpty(userTwos.getTelephone())){
                        if (!StrUtil.equals(userTwos.getTelephone(), addUserVo.getTelephone())) {
//                            failedCount += 1;
//                            addUserVo.setReason("提供的手机号与用户手机号不一致！");
//                            list.add(addUserVo);
                            userTwos.setTelephone(addUserVo.getTelephone());
                            userService.save(userTwos);
                        }
                    }else{
                     // 手机号码为空，补充手机号
                        userTwos.setTelephone(addUserVo.getTelephone());
                        userService.updateById(userTwos);
                    }
                }else{
                    User userTele = userService.getOne(lqwOne);
                    if(userTele != null ){
                        userTele.setIdCardNum(addUserVo.getIdCardNum());
                        userService.save(userTele);
                    }
                }
/*
                if (userService.getOne(lqwOne) == null) {

                    addUserVo.setReason("手机号码不存在");
                    list.add(addUserVo);
                    failedCount += 1;
//                    continue;
                }

                User userOne = userService.getOne(lqwOne);
                if(userOne != null){
                    if (!StrUtil.equals(userOne.getIdCardNum(), addUserVo.getIdCardNum())) {
                        failedCount += 1;
                        addUserVo.setReason("该手机号对应的用户身份证号与提供的不一致");
                        list.add(addUserVo);
//                    continue;
                    }
                }


                if (userService.getOne(lqwOne) == null) {
                    addUserVo.setReason("身份证号码不存在");
                    list.add(addUserVo);
//                    continue;
                }

                User userTwo = userService.getOne(lqwTwo);
                if (!StrUtil.equals(userTwo.getTelephone(), addUserVo.getTelephone())) {
                    failedCount += 1;
                    addUserVo.setReason("该用户身份证号码所对应的手机号与该表提供的手机号码不匹配");
                    list.add(addUserVo);
//                    continue;
                }
*/
//                if (!IdcardUtil.isValidCard(addUserVo.getIdCardNum())) {
//                    failedCount += 1;
//                    addUserVo.setResult("该用户身份证号码不合法");
//                    continue;
//                }
//
//                if (!PhoneUtil.isMobile(addUserVo.getTelephone())) {
//                    failedCount += 1;
//                    addUserVo.setResult("该用户手机号码不合法");
//                    continue;
//                }

                LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
                lqw.eq(StrUtil.isNotEmpty(addUserVo.getTelephone()), User::getTelephone, addUserVo.getTelephone());
//                lqw.eq(StrUtil.isNotEmpty(addUserVo.getIdCardNum()), User::getIdCardNum, addUserVo.getIdCardNum());
                if (userService.getOne(lqw) != null) {
                    LambdaQueryWrapper<Userclass> lqww = new LambdaQueryWrapper<>();
                    lqww.eq(Userclass::getClassId, classId);
                    lqww.eq(Userclass::getUserId, userService.getOne(lqw).getId());
                    if (userclassService.getOne(lqww) != null) {
                        addUserVo.setReason("导入班级课程与该生已加入的班级课程有重复");
                        list.add(addUserVo);
                        failedCount += 1;
                        continue;
                    } else {
                        Userclass userclass = new Userclass();
                        userclass.setUserId(userService.getOne(lqw).getId());
                        userclass.setClassId(classId);

                        boolean b = userclassService.save(userclass);

                        if (b) {
                            successCount += 1;
                        }
                    }
                } else {
                    User user = BeanUtil.toBean(addUserVo, User.class);
                    String gensalt = BCrypt.gensalt();
                    String substring = StrUtil.sub(addUserVo.getIdCardNum(), -6, addUserVo.getIdCardNum().length());
                    String hashpw = BCrypt.hashpw(substring, gensalt);

                    user.setInfo("");
                    user.setEmail("");
                    user.setAccount(addUserVo.getTelephone());
                    user.setPassword(hashpw);
                    user.setName(addUserVo.getRealname());
                    user.setRole("学生");

                    boolean save = userService.save(user);

                    if (save) {
                        Usertag usertag = new Usertag();
                        usertag.setUserId(user.getId());
                        usertag.setTagId(2);

                        Userclass userclass = new Userclass();
                        userclass.setUserId(userService.getOne(lqw).getId());
                        userclass.setClassId(classId);

                        boolean b = userclassService.save(userclass);

                        if (b) {
                            successCount += 1;
                        }

                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        importUserToClassVo.setTotalCount(totalCount);
        importUserToClassVo.setSuccessCount(successCount);
        importUserToClassVo.setFailedCount(failedCount);
        importUserToClassVo.setFailedArr(list);
        return ResultT.success(importUserToClassVo);
    }

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private TaskService taskService;



    @Override
    public StudyListVo studyList(Integer pageNum, Integer pageSize, Integer courseId, Integer classId, String keyword) throws InvocationTargetException, IllegalAccessException {
        Classes classes = classesService.getById(classId);
        StudyListVo setstu = new StudyListVo();
        BeanUtils.copyProperties(setstu,classes);
        Course course = courseService.getById(courseId);
        Integer totalTask = 0;
        //处理数据
        LambdaQueryWrapper<Chapter> chapterQueryWrapper = new LambdaQueryWrapper<>();
        chapterQueryWrapper.eq(Chapter::getCourseId,courseId);
        chapterQueryWrapper.orderByDesc(Chapter::getCreateTime);
        List<Chapter> list = chapterService.list(chapterQueryWrapper);

        if(CollUtil.isNotEmpty(list)){
            for (Chapter chapter : list) {
                LambdaQueryWrapper<Task> taskLambdaQueryWrapper = new LambdaQueryWrapper<>();
                taskLambdaQueryWrapper.eq(Task::getChapterId,chapter.getId());
                taskLambdaQueryWrapper.orderByDesc(Task::getCreateTime);
                List<Task> taskList = taskService.list(taskLambdaQueryWrapper);
                totalTask+=taskList.size();
            }
        }

        List<Taskprogress> taskprogresses = new ArrayList<>();
        if(null != courseId){
            LambdaQueryWrapper<Taskprogress> taskprogressQueryWrapper = new LambdaQueryWrapper<>();
            taskprogressQueryWrapper.in(Taskprogress::getCourseId, courseId);
            taskprogresses = taskprogressMapper.selectList(taskprogressQueryWrapper);
        }

        List<Paperrecord> paperrecords = new ArrayList<>();
        if(null != courseId){
            LambdaQueryWrapper<Paperrecord> paperrecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            paperrecordLambdaQueryWrapper.in(Paperrecord::getCourseId, courseId);
            paperrecords = paperrecordMapper.selectList(paperrecordLambdaQueryWrapper);

        }
        LambdaQueryWrapper<Userclass> qw = new LambdaQueryWrapper<>();
        qw.eq(Userclass::getClassId,classId);
        List<Integer> ins = userclassService.list(qw).stream().map(c -> c.getUserId()).collect(Collectors.toList());
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(keyword)) {
            userLambdaQueryWrapper.and(c -> c.like(User::getName, keyword)
                    .or().like(User::getRealname, keyword)
                    .or().like(User::getGender, keyword)
                    .or().like(User::getIdCardNum, keyword)
                    .or().like(User::getTelephone, keyword)
                    .or().like(User::getInstituteName, keyword)
                    .or().like(User::getMajor, keyword)
            );
        }
        userLambdaQueryWrapper.in(User::getId,ins);
        List<User> userList = userService.list(userLambdaQueryWrapper);

        List<StudentVo> studentVos = new ArrayList<>();
        for (User user : userList) {
            Integer finishedTask = 0;
            if(CollUtil.isNotEmpty(taskprogresses)){
                for (Taskprogress taskprogress : taskprogresses) {
                    if(user.getId().equals(taskprogress.getUserId()) && taskprogress.getIsFinished()){
                        finishedTask +=1;
                    }
                }
            }
            StudentVo studentVo = new StudentVo();
            BeanUtils.copyProperties(studentVo,user);
            studentVo.setFinished(finishedTask);
            studentVo.setTotalTask(totalTask);
            BigDecimal divide = new BigDecimal(BigInteger.ZERO);
            if(totalTask > 0){
                 divide = new BigDecimal(finishedTask).multiply(new BigDecimal(100)).divide(new BigDecimal(totalTask),BigDecimal.ROUND_HALF_UP,2).setScale(2,BigDecimal.ROUND_UP);
            }
           studentVo.setFinishedPercentage(divide.toString()+"%");
            if(CollUtil.isNotEmpty(paperrecords)){
                for (Paperrecord paperrecord : paperrecords) {
                    if(user.getId() == paperrecord.getUserId()){
                        studentVo.setTheoryScore(String.valueOf(paperrecord.getTheoryScore()));
                        studentVo.setOperationScore(String.valueOf(paperrecord.getOperationScore()));
                        studentVo.setPassed(paperrecord.getPassed());
                    }
                }
            }
            studentVos.add(studentVo);

        }
        if(BeanUtil.isNotEmpty(course)){
            setstu.setCourseTitle(course.getTitle());
        }
        setstu.setStudent(studentVos);
        return setstu;
    }


//        if(BeanUtil.isNotEmpty(classes)){
//            LambdaQueryWrapper<Userclass> qw = new LambdaQueryWrapper<>();
//            qw.eq(Userclass::getClassId,classes.getId());
//            List<Integer> ins = userclassService.list(qw).stream().map(c -> c.getUserId()).collect(Collectors.toList());
//            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            userLambdaQueryWrapper.in(User::getId,ins);
//            List<User> list = userService.list(userLambdaQueryWrapper);
//
//            Course course = courseService.getById(courseId);
//
//            LambdaQueryWrapper<Chapter> characterQueryWrapper = new LambdaQueryWrapper<>();
//            characterQueryWrapper.eq(Chapter::getCourseId, courseId).orderByAsc(Chapter::getRank);
//            List<Chapter> chapters = chapterMapper.selectList(characterQueryWrapper);
//            List<Integer> collect1 = chapters.stream().map(Chapter::getId).collect(Collectors.toList());
//
//            LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
//            taskWrapper.in(Task::getChapterId, collect1).orderByAsc(Task::getRank);
//            List<Task> tasks1 = taskMapper.selectList(taskWrapper);
//
//
//            LambdaQueryWrapper<Taskprogress> taskprogressQueryWrapper = new LambdaQueryWrapper<>();
//            taskprogressQueryWrapper.in(Taskprogress::getCourseId, courseId);
//            List<Taskprogress> taskprogresses = taskprogressMapper.selectList(taskprogressQueryWrapper);
//
//            LambdaQueryWrapper<Paperrecord> paperrecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            paperrecordLambdaQueryWrapper.in(Paperrecord::getCourseId, courseId);
//            List<Paperrecord> paperrecords = paperrecordMapper.selectList(paperrecordLambdaQueryWrapper);
//
//
//            List<StudentVo> studentVos = new ArrayList<>();
//            for (User user : list) {
//                Integer finishedTask = 0;
//                if(CollUtil.isNotEmpty(taskprogresses)){
//                    for (Taskprogress taskprogress : taskprogresses) {
//                        if(user.getId().equals(taskprogress.getUserId()) && taskprogress.getIsFinished()){
//                            finishedTask +=1;
//                        }
//                    }
//                }
//                StudentVo studentVo = new StudentVo();
//                BeanUtils.copyProperties(studentVo,user);
//                studentVo.setFinished(finishedTask);
//                studentVo.setTotalTask(tasks1.size());
//                BigDecimal divide = new BigDecimal(finishedTask).multiply(new BigDecimal(100)).divide(new BigDecimal(tasks1.size()),BigDecimal.ROUND_HALF_UP,2).setScale(2,BigDecimal.ROUND_UP);
//                studentVo.setFinishedPercentage(divide.toString()+"%");
//                if(CollUtil.isNotEmpty(paperrecords)){
//                    for (Paperrecord paperrecord : paperrecords) {
//                        if(user.getId() == paperrecord.getUserId()){
//                            studentVo.setTheoryScore(String.valueOf(paperrecord.getTheoryScore()));
//                            studentVo.setOperationScore(String.valueOf(paperrecord.getOperationScore()));
//                            studentVo.setPassed(paperrecord.getPassed());
//                        }
//                    }
//                }
//
//                studentVos.add(studentVo);
//            }
//            setstu.setTitle(course.getTitle());
//            setstu.setStudent(studentVos);
//        }
//        return setstu;




    @Override
    public List<SubAdminVo> listSubAdmin(Integer pageNum, Integer pageSize, String keyword) {

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("user.role","子管理员");
        qw.eq("user.isDelete",0);
        if(StrUtil.isNotEmpty(keyword)){
            qw.like("user.name",keyword);
        }
        List<SubAdminBo> subAdminBos = userMapper.listSubAdmin(qw);
        ArrayList<SubAdminVo> subAdminVos = new ArrayList<>();

        for (SubAdminBo subAdminBo : subAdminBos) {
            SubAdminVo subAdminVo = BeanUtil.copyProperties(subAdminBo, SubAdminVo.class, "routeAuth");
            if (StrUtil.isNotEmpty(subAdminBo.getRouteAuth())) {
                List<String> routeAuth = StrUtil.split(subAdminBo.getRouteAuth(), ",");
                subAdminVo.setRouteAuth(routeAuth);
            }
            subAdminVos.add(subAdminVo);
        }
        return subAdminVos;
    }

}
